package com.example.controller;

import com.example.models.PostBody;
import com.example.models.QueryCondition;
import com.example.models.UserDTO;
import com.example.service.UserDelegate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jonathan
 * 2021/4/24 11:58
 */
@Slf4j
@RestController
public class UserController {

    @Resource
    private UserDelegate userDelegate;

    @GetMapping("/user/{id}")
    public UserDTO getUser(@PathVariable String id) {
        return userDelegate.getUserById(id);
    }

    /**
     * 一般的参数可以放在一个对象里面接受
     * 数组需要单独接收
     * @param ids
     * @param condition
     */
    @GetMapping("/user/test")
    public void getUser1(@RequestParam("ids") List<Integer> ids, QueryCondition condition) {
        log.info("ids:{}", ids);
        log.info("condition:{}", condition);
    }
    @PostMapping("/user/test/post")
    public void getUser2(@RequestBody PostBody postBody) {
        log.info("postBody:{}", postBody);
    }


    @GetMapping("/t1")
    public void testTransactional1() {
        userDelegate.insert1();
    }

    @GetMapping("/t2")
    public void testTransactional2() {
        userDelegate.insert2();
    }

    @GetMapping("/updateData1")
    public void updateData1() {
        userDelegate.updateDataWithDeadLock();
    }

    @GetMapping("/updateData2")
    public void updateData2() {
        userDelegate.updateDataWithoutDeadLock();
    }

    @GetMapping("/updateData3")
    public void updateData3() {
        userDelegate.updateData();
    }

    @GetMapping("/cacheAside")
    public void cacheAside() {
        userDelegate.cacheAsideProblem();
    }

    @GetMapping("/deadLock")
    public void deadLock() {
        userDelegate.deadLockDemo();
    }

}
